<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-card">
        <!-- 标题 -->
        <div class="login-header">
          <h1>{{ $t('user.welcomeBack') || '欢迎回来' }}</h1>
          <p>{{ $t('user.loginToAccount') || '登录您的Web商城账户' }}</p>
        </div>
        
        <!-- 登录表单 -->
        <a-form
          :model="formData"
          :rules="rules"
          @finish="handleLogin"
          layout="vertical"
          class="login-form"
        >
          <a-form-item :label="$t('user.username')" name="username">
            <a-input
              v-model:value="formData.username"
              :placeholder="$t('user.enterUsername') || '请输入用户名'"
              size="large"
            >
              <template #prefix>
                <UserOutlined />
              </template>
            </a-input>
          </a-form-item>
          
          <a-form-item :label="$t('user.password')" name="password">
            <a-input-password
              v-model:value="formData.password"
              :placeholder="$t('user.enterPassword') || '请输入密码'"
              size="large"
            >
              <template #prefix>
                <LockOutlined />
              </template>
            </a-input-password>
          </a-form-item>
          
          <a-form-item>
            <div class="form-options">
              <a-checkbox v-model:checked="rememberMe">
                {{ $t('user.rememberMe') }}
              </a-checkbox>
              <a href="#" class="forgot-password">{{ $t('user.forgotPassword') }}</a>
            </div>
          </a-form-item>
          
          <a-form-item>
            <a-button
              type="primary"
              html-type="submit"
              size="large"
              :loading="loading"
              block
            >
              {{ $t('user.login') }}
            </a-button>
          </a-form-item>
        </a-form>
        
        <!-- 分割线 -->
        <!-- <div class="divider">
          <span>{{ $t('user.or') || '或者' }}</span>
        </div> -->
        
        <!-- 第三方登录 -->
        <!-- <div class="social-login">
          <a-button size="large" block class="social-btn wechat">
            <WechatOutlined />
            微信登录
          </a-button>
          <a-button size="large" block class="social-btn qq">
            <QqOutlined />
            QQ登录
          </a-button>
        </div> -->
        
        <!-- 注册链接 -->
        <div class="register-link">
          <span>{{ $t('user.noAccount') || '还没有账户？' }}</span>
          <a @click="goToRegister">{{ $t('user.registerNow') || '立即注册' }}</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import {
  UserOutlined,
  LockOutlined,
  WechatOutlined,
  QqOutlined
} from '@ant-design/icons-vue'

// 使用Vuex store和Vue Router
const store = useStore()
const router = useRouter()
const route = useRoute()

// 响应式数据
const loading = ref(false)
const rememberMe = ref(false)

const formData = reactive({
  username: 'admin',
  password: '123456'
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' }
  ]
}

// 方法
const handleLogin = async (values) => {
  loading.value = true
  
  try {
    const result = await store.dispatch('user/login', values)
    
    if (result.success) {
      message.success(result.message)
      
      // 跳转到之前的页面或首页
      const redirect = route.query.redirect || '/'
      router.push(redirect)
    } else {
      console.log(result)
      message.error(result.message)
    }
  } catch (error) {
    console.error('登录失败:', error)
    message.error('登录失败，请重试')
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin: 0 0 8px 0;
}

.login-header p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.forgot-password {
  color: #1890ff;
  text-decoration: none;
  font-size: 14px;
}

.forgot-password:hover {
  color: #40a9ff;
}

.divider {
  position: relative;
  text-align: center;
  margin: 20px 0;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #f0f0f0;
}

.divider span {
  background: white;
  padding: 0 15px;
  color: #999;
  font-size: 14px;
}

.social-login {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
}

.social-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 6px;
  font-weight: 500;
}

.social-btn.wechat {
  background-color: #07c160;
  border-color: #07c160;
  color: white;
}

.social-btn.wechat:hover {
  background-color: #06ad56;
  border-color: #06ad56;
}

.social-btn.qq {
  background-color: #12b7f5;
  border-color: #12b7f5;
  color: white;
}

.social-btn.qq:hover {
  background-color: #0fa3d9;
  border-color: #0fa3d9;
}

.register-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.register-link a {
  color: #1890ff;
  text-decoration: none;
  margin-left: 5px;
  cursor: pointer;
}

.register-link a:hover {
  color: #40a9ff;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }
  
  .login-header h1 {
    font-size: 24px;
  }
  
  .form-options {
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
  }
}
</style> 